// Copyright tang.  All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package middle.ground.controller.converter;

import middle.ground.common.converter.AbstractConverter;
import middle.ground.dao.AssignmentConfigDAO;
import middle.ground.dao.DatabaseConnectionDAO;
import middle.ground.entity.AssignmentConfigEntity;
import middle.ground.entity.AssignmentTaskEntity;
import middle.ground.entity.DatabaseConnectionEntity;
import middle.ground.model.response.AssignmentDetailResponse;
import middle.ground.type.IncludeExcludeEnum;
import middle.ground.util.SpringUtils;

import java.util.Objects;

public class AssignmentDetailConverter extends
		AbstractConverter<AssignmentTaskEntity, AssignmentDetailResponse> {

	@Override
	public AssignmentDetailResponse convert(AssignmentTaskEntity assignmentTaskEntity) {
		if (Objects.isNull(assignmentTaskEntity)) {
			return null;
		}

		DatabaseConnectionDAO databaseConnectionDAO = SpringUtils.getBean(DatabaseConnectionDAO.class);
		AssignmentConfigDAO assignmentConfigDAO = SpringUtils.getBean(AssignmentConfigDAO.class);
		AssignmentConfigEntity taskConfig = assignmentConfigDAO.getByAssignmentTaskId(
				assignmentTaskEntity.getId());
		DatabaseConnectionEntity srcConn = databaseConnectionDAO
				.getById(taskConfig.getSourceConnectionId());
		DatabaseConnectionEntity dstConn = databaseConnectionDAO
				.getById(taskConfig.getTargetConnectionId());

		AssignmentDetailResponse.Configuration config = new AssignmentDetailResponse.Configuration();
		config.setSourceConnectionId(srcConn.getId());
		config.setSourceConnectionName(srcConn.getName());
		config.setSourceSchema(taskConfig.getSourceSchema());
		config.setIncludeOrExclude(taskConfig.getExcluded()
				? IncludeExcludeEnum.EXCLUDE
				: IncludeExcludeEnum.INCLUDE);
		config.setSourceTables(taskConfig.getSourceTables());
		config.setTargetConnectionId(dstConn.getId());
		config.setTargetConnectionName(dstConn.getName());
		config.setTargetSchema(taskConfig.getTargetSchema());
		config.setTargetOnlyCreate(taskConfig.getTargetOnlyCreate());
		config.setTargetDropTable(taskConfig.getTargetDropTable());
		config.setTargetSyncExit(taskConfig.getTargetSyncExit());
		config.setTargetIndexCreate(taskConfig.getTargetIndexCreate());
		config.setTargetLowerCase(taskConfig.getTargetLowerCase());
		config.setTargetUpperCase(taskConfig.getTargetUpperCase());
		config.setTargetAutoIncrement(taskConfig.getTargetAutoIncrement());
		config.setTargetDataSync(taskConfig.getTargetDataSync());
		config.setTableNameMapper(taskConfig.getTableNameMap());
		config.setColumnNameMapper(taskConfig.getColumnNameMap());

		config.setBatchSize(taskConfig.getBatchSize());

		AssignmentDetailResponse detailResponse = new AssignmentDetailResponse();
		detailResponse.setId(assignmentTaskEntity.getId());
		detailResponse.setName(assignmentTaskEntity.getName());
		detailResponse.setDescription(assignmentTaskEntity.getDescription());
		detailResponse.setScheduleMode(assignmentTaskEntity.getScheduleMode());
		detailResponse.setCronExpression(assignmentTaskEntity.getCronExpression());
		detailResponse.setCreateTime(assignmentTaskEntity.getCreateTime());
		detailResponse.setUpdateTime(assignmentTaskEntity.getUpdateTime());
		detailResponse.setConfiguration(config);

		return detailResponse;
	}

}
